#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/builder-nixpacks/internal-functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

trigger-builder-nixpacks-builder-build() {
  declare desc="builder-nixpacks builder-build plugin trigger"
  declare trigger="builder-build"
  declare BUILDER_TYPE="$1" APP="$2" SOURCECODE_WORK_DIR="$3"

  if [[ "$BUILDER_TYPE" != "nixpacks" ]]; then
    return
  fi

  dokku_log_info1 "Building $APP from nixpacks"

  if ! command -v "nixpacks" &>/dev/null; then
    dokku_log_fail "Missing nixpacks, install it"
  fi

  local IMAGE=$(get_app_image_name "$APP")
  local DOCKER_BUILD_LABEL_ARGS=("--label=dokku" "--label=org.label-schema.schema-version=1.0" "--label=org.label-schema.vendor=dokku" "--label=com.dokku.image-stage=build" "--label=com.dokku.builder-type=nixpacks" "--label=com.dokku.app-name=$APP")

  pushd "$SOURCECODE_WORK_DIR" &>/dev/null

  plugn trigger pre-build "$BUILDER_TYPE" "$APP" "$SOURCECODE_WORK_DIR"

  if [[ -f "$SOURCECODE_WORK_DIR/Procfile" ]]; then
    if procfile-util exists --process-type release; then
      procfile-util delete --process-type release
    fi
  fi

  local DOCKER_ARGS=$(: | plugn trigger docker-args-build "$APP" "$BUILDER_TYPE")
  DOCKER_ARGS+=$(: | plugn trigger docker-args-process-build "$APP" "$BUILDER_TYPE")
  DOCKER_ARGS+=" $DOKKU_GLOBAL_BUILD_ARGS"

  DOCKER_ARGS=" $DOCKER_ARGS "
  eval set -- "$DOCKER_ARGS"

  declare -a NIXPACKS_ARGS
  while true; do
    case "$1" in
      -t | --tag)
        NIXPACKS_ARGS+=("--tag")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      -t=* | --tag=*)
        if [[ "$1" == "--tag=*" ]]; then
          NIXPACKS_ARGS+=("--tag" "${1#--tag=}")
        elif [[ "$1" == "-t=*" ]]; then
          NIXPACKS_ARGS+=("--tag" "${1#-t=}")
        fi
        shift 1
        ;;
      -i | --install-cmd)
        NIXPACKS_ARGS+=("--install-cmd")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      -i=* | --install-cmd=*)
        if [[ "$1" == "--install-cmd=*" ]]; then
          NIXPACKS_ARGS+=("--install-cmd" "${1#--install-cmd=}")
        elif [[ "$1" == "-i=*" ]]; then
          NIXPACKS_ARGS+=("--install-cmd" "${1#-i=}")
        fi
        shift 1
        ;;
      -l | --label)
        NIXPACKS_ARGS+=("--label")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      -l=* | --label=*)
        if [[ "$1" == "--label=*" ]]; then
          NIXPACKS_ARGS+=("--label" "${1#--label=}")
        elif [[ "$1" == "-l=*" ]]; then
          NIXPACKS_ARGS+=("--label" "${1#-l=}")
        fi
        shift 1
        ;;
      -b | --build-cmd)
        NIXPACKS_ARGS+=("--build-cmd")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      -b=* | --build-cmd=*)
        if [[ "$1" == "--build-cmd=*" ]]; then
          NIXPACKS_ARGS+=("--build-cmd" "${1#--build-cmd=}")
        elif [[ "$1" == "-b=*" ]]; then
          NIXPACKS_ARGS+=("--build-cmd" "${1#-b=}")
        fi
        shift 1
        ;;
      -s | --start-cmd)
        NIXPACKS_ARGS+=("--start-cmd")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      -s=* | --start-cmd=*)
        if [[ "$1" == "--start-cmd=*" ]]; then
          NIXPACKS_ARGS+=("--start-cmd" "${1#--start-cmd=}")
        elif [[ "$1" == "-s=*" ]]; then
          NIXPACKS_ARGS+=("--start-cmd" "${1#-s=}")
        fi
        shift 1
        ;;
      -p | --pkgs)
        NIXPACKS_ARGS+=("--pkgs")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      -p=* | --pkgs=*)
        if [[ "$1" == "--pkgs=*" ]]; then
          NIXPACKS_ARGS+=("--pkgs" "${1#--pkgs=}")
        elif [[ "$1" == "-p=*" ]]; then
          NIXPACKS_ARGS+=("--pkgs" "${1#-p=}")
        fi
        shift 1
        ;;
      -a | --apt)
        NIXPACKS_ARGS+=("--apt")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      -a=* | --apt=*)
        if [[ "$1" == "--apt=*" ]]; then
          NIXPACKS_ARGS+=("--apt" "${1#--apt=}")
        elif [[ "$1" == "-a=*" ]]; then
          NIXPACKS_ARGS+=("--apt" "${1#-a=}")
        fi
        shift 1
        ;;
      -e | --env)
        NIXPACKS_ARGS+=("--env")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      -e=* | --env=*)
        if [[ "$1" == "--env=*" ]]; then
          NIXPACKS_ARGS+=("--env" "${1#--env=}")
        elif [[ "$1" == "-e=*" ]]; then
          NIXPACKS_ARGS+=("--env" "${1#-e=}")
        fi
        shift 1
        ;;
      --platform)
        NIXPACKS_ARGS+=("$1")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      --platform=*)
        NIXPACKS_ARGS+=("$1")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      --cache-key)
        NIXPACKS_ARGS+=("$1")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      --cache-key=*)
        NIXPACKS_ARGS+=("$1")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      --incremental-cache-image)
        NIXPACKS_ARGS+=("$1")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      --incremental-cache-image=*)
        NIXPACKS_ARGS+=("$1")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      --libs)
        NIXPACKS_ARGS+=("$1")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      --libs=*)
        NIXPACKS_ARGS+=("$1")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      --cache-from)
        NIXPACKS_ARGS+=("$1")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      --cache-from=*)
        NIXPACKS_ARGS+=("$1")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      --docker-host)
        NIXPACKS_ARGS+=("$1")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      --docker-host=*)
        NIXPACKS_ARGS+=("$1")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      --add-host)
        NIXPACKS_ARGS+=("$1")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      --add-host=*)
        NIXPACKS_ARGS+=("$1")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      --docker-tls-verify)
        NIXPACKS_ARGS+=("$1")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      --docker-tls-verify=*)
        NIXPACKS_ARGS+=("$1")
        NIXPACKS_ARGS+=("$2")
        shift 2
        ;;
      --no-cache | --inline-cache | --no-error-without-start)
        NIXPACKS_ARGS+=("$1")
        shift 1
        ;;
      *)
        if [[ -n "$1" ]]; then
          shift
          continue
        fi
        break
        ;;
    esac
  done

  eval "$(config_export app "$APP" --merged)"

  # shellcheck disable=SC2086
  if ! nixpacks build "${DOCKER_BUILD_LABEL_ARGS[@]}" "${NIXPACKS_ARGS[@]}" --name "$IMAGE" "$SOURCECODE_WORK_DIR"; then
    dokku_log_warn "Failure building image"
    return 1
  fi

  if ! suppress_output "$DOCKER_BIN" image build -f "$PLUGIN_AVAILABLE_PATH/builder-nixpacks/dockerfiles/builder-build.Dockerfile" --build-arg APP_IMAGE="$IMAGE" -t "$IMAGE" "$SOURCECODE_WORK_DIR"; then
    dokku_log_warn "Failure injecting docker labels and custom entrypoint on image"
    return 1
  fi

  plugn trigger post-build "$BUILDER_TYPE" "$APP" "$SOURCECODE_WORK_DIR"
  popd &>/dev/null || pushd "/tmp" >/dev/null
}

trigger-builder-nixpacks-builder-build "$@"
